package tq.com.tqteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import tq.com.systools.system.StringHelper;
import tq.com.tqteacher.R;

public class TQQNoManagerView extends LinearLayout {
    private boolean readOnly = false;

    public TQQNoManagerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TQQNoManagerView);
        readOnly = typedArray.getBoolean(R.styleable.TQQNoManagerView_TQQNoManagerView_readonly, false);
        typedArray.recycle();

        addRegion();
    }

    public TQRegionItemView addRegion(){
        TQRegionItemView lItemView = new TQRegionItemView(getContext(), null);
        LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(lItemView, lParams);

        if(readOnly){
            lItemView.setReadOnly();
        }
        return lItemView;
    }

    public void remove(TQRegionItemView pItemView){
        if(getChildCount() <= 1) return;

        removeView(pItemView);
    }

    public ArrayList<TQRegionData> getData(){
        int count = getChildCount();

        ArrayList<TQRegionData> lRegionData = new ArrayList<>();
        for (int i = 0; i < count; i++){
            View lView = getChildAt(i);
            if(lView instanceof TQRegionItemView){
                TQRegionData lData = ((TQRegionItemView)lView).getData();
                if(lData != null){
                    lRegionData.add(lData);
                }
            }
        }
        return lRegionData;
    }

    public boolean checkDataValid(){
        int count = getChildCount();

        ArrayList<TQRegionData> lRegionData = new ArrayList<>();
        for (int i = 0; i < count; i++){
            View lView = getChildAt(i);
            if(lView instanceof TQRegionItemView){
                TQRegionData lData = ((TQRegionItemView)lView).getData();
                if(lData == null) continue;

                if(!isValidRegion(lData, lRegionData)){
                    return false;
                }else{
                    lRegionData.add(lData);
                }
            }
        }
        return true;
    }
    private boolean isValidRegion(TQRegionData pData, ArrayList<TQRegionData> sets){
        if(sets == null || sets.size() == 0) return true;

        int s = Integer.valueOf(pData.start);
        int e = Integer.valueOf(pData.end);

        for (TQRegionData l: sets) {
            int tmpS = Integer.valueOf(l.start);
            int tmpE = Integer.valueOf(l.end);

            if(s >= tmpS && s <= tmpE){
                return false;
            }
            if(e >= tmpS && e <= tmpE){
                return false;
            }
            if(tmpS >= s && tmpS <= e){
                return false;
            }
            if(tmpE >= s && tmpE <= e){
                return false;
            }
        }

        return true;
    }
    public void setData(ArrayList<TQRegionData> pData){
        if(pData == null) return;

//        clearAll();
        removeAllViews();
        for (int i = 0; i < pData.size(); i++){
            TQRegionItemView lItemView = addRegion();

            lItemView.bindData(pData.get(i));
        }
    }

    private void clearAll(){
        for (int i = 0; i < getChildCount(); i++){
            View lView = getChildAt(i);
            if(lView instanceof TQRegionItemView){
                removeView(lView);
            }
        }
    }

    public class TQRegionItemView extends FrameLayout{
        private EditText startInput;
        private EditText endInput;
        private ImageView delFlag;

        public TQRegionItemView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initUi();
        }

        public void setReadOnly(){
            startInput.setEnabled(false);
            endInput.setEnabled(false);

            delFlag.setVisibility(GONE);
        }

        private void initUi(){
            View lView = LayoutInflater.from(getContext()).inflate(R.layout.view_qnoregion_layout, this, true);
            lView.findViewById(R.id.del_flag).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    remove(TQRegionItemView.this);
                }
            });

            startInput = findViewById(R.id.start_input);
            endInput = findViewById(R.id.end_input);
            delFlag =findViewById(R.id.del_flag);
        }

        public TQRegionData getData(){
            String s = StringHelper.editText(startInput);
            String e = StringHelper.editText(endInput);
            if(StringHelper.isEmpty(s) || StringHelper.isEmpty(e)){
                return null;
            }

            if(!StringHelper.isNumeric(s) || !StringHelper.isNumeric(e)){
                return null;
            }

            if(Integer.valueOf(s) < 0 || Integer.valueOf(e) < 0 || Integer.valueOf(s) > Integer.valueOf(e)){
                return null;
            }


            TQRegionData lData = new TQRegionData();
            lData.start = s;
            lData.end = e;
            return lData;
        }

        public void bindData(TQRegionData pData){
            if(pData == null) return;

            startInput.setText(pData.start);
            endInput.setText(pData.end);
        }
    }

    static public class TQRegionData{
        public String start;
        public String end;

    }

}
